package com.fas.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.fas.system.domain.FasBuildingCategory;
import com.fas.system.mapper.FasBuildingCategoryMapper;
import com.fas.system.service.IFasBuildingCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasBuildingCategoryServiceImpl implements IFasBuildingCategoryService {

    @Resource
    private FasBuildingCategoryMapper fasBuildingCategoryMapper;

    @Override
    public List<Tree<String>> dicCompanyCategory(){
        List<FasBuildingCategory> list = fasBuildingCategoryMapper.findAll();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setChildrenKey("children");
        //转换器
        return TreeUtil.build(list, "null", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getName());
                });
    }
}
